package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BookTypeDao;
import domain.BookType;


/**
 * Servlet implementation class EditBookType
 */
public class EditBookType extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditBookType() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		BookTypeDao dao = new BookTypeDao();
		BookType bookType = dao.getByTypeId(request.getParameter("typeId"));
		request.setAttribute("bookType", bookType);
		RequestDispatcher rd = request
				.getRequestDispatcher("/WEB-INF/jsp/editBookType.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		BookType newBookType = new BookType(request.getParameter("typeId"),
				request.getParameter("typeName"));
		BookTypeDao dao = new BookTypeDao();
		dao.update(request.getParameter("oldTypeId"), newBookType);
		response.sendRedirect(request.getContextPath()+"/admin/ListBookType");

	}

}
